'''
This file is part of ModeliMark.

ModeliMark is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.

ModeliMark is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with ModeliMark.
If not, see <http://www.gnu.org/licenses/>.

Copyright 2012 Jens Frenkel
'''

import loadResultFile
import matplotlib.pyplot as plt

class Chart(object):
    '''
    classdocs
    '''


    def __init__(self, file, title, picture, lines=[1,1,1,1]):
        '''
        Constructor
        '''
        assert(isinstance(file,str))
        assert(isinstance(title,str))
        assert(isinstance(picture,str))

        self.file = file
        self.title = title
        self.picture = picture

        data = loadResultFile.LoadResultFile(self.file)

        legend = list();
        if (lines[0] == 1):
            plt.plot(data.simulate[0], data.simulate[1])
            legend.append('simulate')
        if (lines[1] == 1):
            plt.plot(data.translate[0], data.translate[1])
            legend.append('translate')
        if (lines[2] == 1):
            plt.plot(data.flatten[0], data.flatten[1])
            legend.append('flatten')
        if (lines[3] == 1):
            plt.plot(data.open[0], data.open[1])
            legend.append('open')
        plt.legend( legend ,loc='upper left')
        plt.ylabel('time[s]')
        plt.xlabel('number of equations')
        plt.title(title)
        plt.legend()
        plt.savefig(picture)




class ComparisonChart(object):
    '''
    classdocs
    '''


    def __init__(self, files, title, picture, lines=[1,1,1,1],size=0.6):
        '''
        Constructor
        '''
        assert(isinstance(files,list))
        assert(isinstance(title,str))
        assert(isinstance(picture,str))

        self.files = files
        self.title = title
        self.picture = picture

        fig = plt.figure()
        ax = plt.subplot(111)

        legend = list();
        for filetpl in files:
            file = filetpl[1]
            data = loadResultFile.LoadResultFile(file)

            if (lines[0] == 1):
                ax.plot(data.simulate[0], data.simulate[1], label='simulate_%s'%(filetpl[0]))
            if (lines[1] == 1):
                ax.plot(data.translate[0], data.translate[1], label='translate_%s'%(filetpl[0]))
            if (lines[2] == 1):
                ax.plot(data.flatten[0], data.flatten[1], label='flatten_%s'%(filetpl[0]))
            if (lines[3] == 1):
                ax.plot(data.open[0], data.open[1], label='open_%s'%(filetpl[0]))


        # Shink current axis by 20%
        box = ax.get_position()
        ax.set_position([box.x0, box.y0, box.width * size, box.height])

        ax.legend( loc='center left', bbox_to_anchor=(1, 0.5))
        plt.ylabel('time[s]')
        plt.xlabel('number of equations')
        plt.title(title)
        plt.savefig(picture)


class ComparisonChartMSL(object):
    '''
    classdocs
    '''


    def __init__(self, files, title, picture, lines=[1,1,1,1],size=0.6):
        '''
        Constructor
        '''
        assert(isinstance(files,list))
        assert(isinstance(title,str))
        assert(isinstance(picture,str))

        self.files = files
        self.title = title
        self.picture = picture

        fig = plt.figure()
        ax = plt.subplot(111)

        legend = dict();
        for filetpl in files:
            file = filetpl[1]
            data = loadResultFile.LoadResultFile(file)

            key = str(data.simulate[0])
            if (key not in legend):
                legend[key] = len(legend)
            key = legend[key]
            if (lines[0] == 1):
                ax.plot(key, data.simulate[1], label='simulate_%s'%(filetpl[0]))
            if (lines[1] == 1):
                ax.plot(key, data.translate[1], label='translate_%s'%(filetpl[0]))
            if (lines[2] == 1):
                ax.plot(key, data.flatten[1], label='flatten_%s'%(filetpl[0]))
            if (lines[3] == 1):
                ax.plot(key, data.open[1], label='open_%s'%(filetpl[0]))


        # Shink current axis by 20%
        box = ax.get_position()
        ax.set_position([box.x0, box.y0, box.width * size, box.height])

        ax.legend( loc='center left', bbox_to_anchor=(1, 0.5))
        plt.ylabel('time[s]')
        plt.xlabel('number of equations')
        plt.title(title)
        plt.savefig(picture)